unit uSelectInfo;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, cxPCdxBarPopupMenu, cxPC, cxGroupBox,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, Data.DB,
  cxDBData, cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, Vcl.StdCtrls, cxTextEdit,
  Vcl.Menus, cxButtons, Datasnap.DBClient, ClientRowSet, cxCheckBox;

type
  TFrmSelectInfo = class(TForm)
    pgList: TcxPageControl;
    TabNode: TcxTabSheet;
    cxGroupBox2: TcxGroupBox;
    GrdNode: TcxGrid;
    tvNode: TcxGridDBTableView;
    lvEXP_COMP: TcxGridLevel;
    Label1: TLabel;
    edtNode: TcxTextEdit;
    cxButton11: TcxButton;
    btnNodeConfirm: TcxButton;
    crsNode: TClientRowSet;
    dsNode: TDataSource;
    crsNodeSYS_GROUP_COMP_ID: TStringField;
    crsNodeNODE_NAME: TStringField;
    crsNodeNODE_NO: TStringField;
    tvNodeNODE_NAME: TcxGridDBColumn;
    tvNodeNODE_NO: TcxGridDBColumn;
    TabCust: TcxTabSheet;
    cxGroupBox1: TcxGroupBox;
    Label2: TLabel;
    edtEmpNo: TcxTextEdit;
    cxButton1: TcxButton;
    btnEmpConfirm: TcxButton;
    GrdEmp: TcxGrid;
    tvEmp: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    crsEmp: TClientRowSet;
    dsEmp: TDataSource;
    crsEmpEMPLOYEE_ID: TStringField;
    crsEmpEMP_NO: TStringField;
    crsEmpEMP_NAME: TStringField;
    tvEmpEMP_NO: TcxGridDBColumn;
    tvEmpEMP_NAME: TcxGridDBColumn;
    cxTabSheet1: TcxTabSheet;
    crsComType: TClientRowSet;
    dsComType: TDataSource;
    crsComTypeID: TStringField;
    crsComTypeFIRST_ID: TStringField;
    crsComTypeSYS_ACCOUNT: TStringField;
    crsComTypeGROUP_CODE: TStringField;
    crsComTypeCOM_TYPE: TStringField;
    crsComTypeERROR_TYPE: TStringField;
    crsComTypeRESPON_LEVEL: TStringField;
    crsComTypeENABLED: TSmallintField;
    crsComTypeVERSION: TIntegerField;
    crsComTypeSTATUS: TSmallintField;
    crsComTypeREMARK: TStringField;
    crsComTypeMEMO: TStringField;
    crsComTypeCREATE_BY: TStringField;
    crsComTypeCREATE_DT: TStringField;
    crsComTypeUPDATE_DT: TStringField;
    crsComTypeCHANNEL_ID: TStringField;
    cxGroupBox3: TcxGroupBox;
    btnComType: TcxButton;
    GrdComType: TcxGrid;
    tvComType: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    tvComTypeERROR_TYPE: TcxGridDBColumn;
    tvComTypeRESPON_LEVEL: TcxGridDBColumn;
    crsEmpNODE_NO: TStringField;
    tvEmpNODE_NO: TcxGridDBColumn;
    cxTabSheet2: TcxTabSheet;
    cxGrid1: TcxGrid;
    tvClass: TcxGridDBTableView;
    cxGridLevel3: TcxGridLevel;
    dsWorkClass: TDataSource;
    crsWorkClass: TClientRowSet;
    crsWorkClassCLASS_ID: TStringField;
    crsWorkClassCLASS_NAME: TStringField;
    crsWorkClassCLASS_TYPE: TStringField;
    crsWorkClassCOM_START: TStringField;
    crsWorkClassCOM_CLOSE: TStringField;
    crsWorkClassNODE_NO: TStringField;
    crsWorkClassRCV_LAST: TStringField;
    tvClassCLASS_NAME: TcxGridDBColumn;
    tvClassCLASS_TYPE: TcxGridDBColumn;
    tvClassNODE_NO: TcxGridDBColumn;
    cxGroupBox4: TcxGroupBox;
    btnClassConfirm: TcxButton;
    crsNodeCHECKED: TBooleanField;
    tvNodeCHECKED: TcxGridDBColumn;
    cxButton2: TcxButton;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure cxButton11Click(Sender: TObject);
    procedure tvNodeDblClick(Sender: TObject);
    procedure btnNodeConfirmClick(Sender: TObject);
    procedure btnEmpConfirmClick(Sender: TObject);
    procedure tvEmpDblClick(Sender: TObject);
    procedure tvEmpEditDblClick(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
    procedure tvNodeEditDblClick(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
    procedure btnComTypeClick(Sender: TObject);
    procedure tvComTypeDblClick(Sender: TObject);
    procedure tvComTypeEditDblClick(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
    procedure cxButton1Click(Sender: TObject);
    procedure btnClassConfirmClick(Sender: TObject);
    procedure tvClassDblClick(Sender: TObject);
    procedure tvClassEditDblClick(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
    procedure cxButton2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    nodeTemp:string;
    classType:string;
    nodeNo:string;
    isShowAll:Boolean;
    procedure SearchNode;
    procedure SearchEmp;
    procedure SearchClass;
    procedure SetTabSheetVisible(pIndex: Integer);
  public
    { Public declarations }
  end;

var
  FrmSelectInfo: TFrmSelectInfo;
  function SelectInfoNodeShow: string;
  function SelectEmpShow: string;
  procedure SelectClassEx(class_type:string;var class_name, class_id: string;node_No:string='');
  procedure SelectEmpShowEx(var empNo, empName: string;node:string='');
  procedure SelectResponShow(comType: string; var errorType, responLevel: string);
  function SelectInfoNodeExShow(var sNodeName:string): string;
  function SelectBatchNodeInfoShow(var sList:TStringList):Boolean;

implementation
uses Console, uDm, ucommFunction, BaseForm;
{$R *.dfm}

function SelectBatchNodeInfoShow(var sList:TStringList):Boolean;
var
  I:Integer;
begin
  Result := False;
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  begin
    tvNodeCHECKED.Visible:=True;
    cxButton2.Visible:=True;
    SearchNode;
    SetTabSheetVisible(0);
    tvNode.OnDblClick:=nil;
    if ShowModal=mrOK then
    begin
      for I := 1 to crsNode.RecordCount do
      begin
        crsNode.RecNo:=I;
        if crsNode.FieldByName('checked').AsBoolean then
          sList.Add(crsNode.FieldByName('node_no').AsString);
      end;
      Result:=True;
    end;
  end;
end;

function SelectInfoNodeExShow(var sNodeName:string): string;
begin
  Result := '';
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  try
    SearchNode;
    SetTabSheetVisible(0);
    if ShowModal = mrOk then
    begin
      sNodeName:= crsNode.FieldByName('node_name').AsString;
      Result := crsNode.FieldByName('node_no').AsString;
    end;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;

procedure TFrmSelectInfo.btnNodeConfirmClick(Sender: TObject);
begin
  if crsNode.RecordCount > 0 then
    ModalResult := mrOk;
end;

procedure TFrmSelectInfo.cxButton11Click(Sender: TObject);
begin
  SearchNode;
end;

procedure TFrmSelectInfo.cxButton1Click(Sender: TObject);
begin
  SearchEmp;
end;

procedure TFrmSelectInfo.cxButton2Click(Sender: TObject);
var
  I:Integer;
begin
  for I := 1 to crsNode.RecordCount do
  begin
    crsNode.RecNo:=I;
    crsNode.Edit;
    crsNode.FieldByName('checked').AsBoolean:=True;
    crsNode.Post;
  end;
end;

procedure TFrmSelectInfo.tvClassDblClick(Sender: TObject);
begin
  btnClassConfirm.Click;
end;

procedure TFrmSelectInfo.tvClassEditDblClick(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
begin
  btnClassConfirm.Click;
end;

procedure TFrmSelectInfo.btnClassConfirmClick(Sender: TObject);
begin
  if crsWorkClass.RecordCount>0 then
    ModalResult := mrOk;
end;

procedure TFrmSelectInfo.btnComTypeClick(Sender: TObject);
begin
  if crsComType.RecordCount > 0 then
    ModalResult := mrOk;
end;

procedure TFrmSelectInfo.btnEmpConfirmClick(Sender: TObject);
begin
  if crsEmp.RecordCount > 0 then
    ModalResult := mrOk;
end;

procedure TFrmSelectInfo.FormCreate(Sender: TObject);
begin
  pSetCaption(Self);
end;

procedure TFrmSelectInfo.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    SelectNext(ActiveControl, True, True);
end;

procedure TFrmSelectInfo.SearchClass;
var
  tmpStr: string;
begin
  tmpStr := 'SELECT ' + GetClientRowSetColumns(crsWorkClass) + ' FROM bas_work_class WHERE group_code=' + QuotedStr(userInfo.groupCode);
  if not userInfo.isCenter then
    tmpStr:=tmpStr+' and Node_NO in '+userInfo.NodeStr;
  if classType<>'' then
    tmpStr:=tmpStr+' and class_type='+QuotedStr(classType);
  if nodeNo<>'' then
    tmpStr:=tmpStr+' and node_no='+QuotedStr(nodeNo);
  OpenDataSet(tmpStr, crsWorkClass);
end;

procedure TFrmSelectInfo.SearchEmp;
var
  tmpStr,sNodeList: string;
begin
  tmpStr := 'SELECT ' + GetClientRowSetColumns(crsEmp) + ' FROM bas_employee WHERE group_code=' + QuotedStr(userInfo.groupCode) +
    ' AND disabled=0 AND state=0';
  if not userInfo.isCenter then
    tmpStr:=tmpStr+' and Node_NO in '+userInfo.NodeStr;
  if nodeTemp<>'' then
    tmpStr:=tmpStr+' and Node_NO = '+QuotedStr(nodeTemp);
  if Trim(edtEmpNo.Text) <> '' then
    tmpStr := tmpStr + ' AND (emp_no LIKE ' + QuotedStr('%' + edtEmpNo.Text + '%') +
      ' OR emp_name LIKE ' + QuotedStr('%' + edtEmpNo.Text + '%') + ')';
  if not userInfo.isCenter then
  begin
    sNodeList:=fGetNodeList(userInfo.nodeNo);
    tmpStr:=tmpStr+' and node_no in '+sNodeList;
  end;
  OpenDataSet(tmpStr, crsEmp);
end;

procedure TFrmSelectInfo.SearchNode;
var
  tmpStr,sNodeList: string;
begin
  tmpStr := 'SELECT ' + GetClientRowSetColumns(crsNode) + ' FROM sys_group_node WHERE group_code=' + QuotedStr(userInfo.groupCode) +
    ' AND enabled=1';
  if Trim(edtNode.Text) <> '' then
    tmpStr := tmpStr + ' AND (node_no LIKE ' + QuotedStr('%' + edtNode.Text + '%') +
      ' OR node_name LIKE ' + QuotedStr('%' + edtNode.Text + '%') + ')';
  tmpStr:=tmpStr+' and '+fGetDataScope('node_no');
//  if not userInfo.isCenter then
//  begin
//    sNodeList:=fGetNodeList(userInfo.nodeNo);
//    tmpStr:=tmpStr+' and node_no in '+sNodeList;
//  end;
  OpenDataSet(tmpStr, crsNode);
end;

procedure TFrmSelectInfo.SetTabSheetVisible(pIndex: Integer);
var
  I: Integer;
begin
  for I := 0 to pgList.PageCount - 1 do
  if I <> pIndex then
    pgList.Pages[I].TabVisible := False
  else
    pgList.Pages[I].TabVisible := True;
  pgList.ActivePageIndex := pIndex;
end;

procedure TFrmSelectInfo.tvComTypeDblClick(Sender: TObject);
begin
  btnComType.Click;
end;

procedure TFrmSelectInfo.tvComTypeEditDblClick(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
begin
  btnComType.Click;
end;

procedure TFrmSelectInfo.tvEmpDblClick(Sender: TObject);
begin
  btnEmpConfirm.Click;
end;

procedure TFrmSelectInfo.tvEmpEditDblClick(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
begin
  btnEmpConfirm.Click;
end;

procedure TFrmSelectInfo.tvNodeDblClick(Sender: TObject);
begin
  btnNodeConfirm.Click;
end;

procedure TFrmSelectInfo.tvNodeEditDblClick(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
begin
  btnNodeConfirm.Click;
end;

function SelectInfoNodeShow: string;
begin
  Result := '';
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  try
    SearchNode;
    SetTabSheetVisible(0);
    if ShowModal = mrOk then
      Result := crsNode.FieldByName('node_no').AsString;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;

function SelectEmpShow: string;
begin
  Result := '';
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  try
    SearchEmp;
    SetTabSheetVisible(1);
    //pgList.ActivePageIndex := 1;
    if ShowModal = mrOk then
      Result := crsEmp.FieldByName('emp_no').AsString;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;

procedure SelectEmpShowEx(var empNo, empName: string;node:string='');
begin
  empNo := '';
  empName := '';
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  try
    nodeTemp:=node;
    SearchEmp;
    SetTabSheetVisible(1);
    if ShowModal = mrOk then
    begin
      empNo := crsEmp.FieldByName('emp_no').AsString;
      empName := crsEmp.FieldByName('emp_name').AsString;
    end;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;

procedure SelectClassEx(class_type:string;var class_name, class_id: string;node_No:string);
begin
  class_name := '';
  class_id := '';
  Application.CreateForm(TFrmSelectInfo, frmSelectInfo);
  with FrmSelectInfo do
  try
    classType:=class_type;
    nodeNo:=node_No;
    SearchClass;
    SetTabSheetVisible(3);
    if ShowModal = mrOk then
    begin
      class_name := crsWorkClass.FieldByName('class_name').AsString;
      class_id := crsWorkClass.FieldByName('class_id').AsString;
    end;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;

procedure SelectResponShow(comType: string; var errorType, responLevel: string);
begin
  errorType := '';
  responLevel := '';
  Application.CreateForm(TFrmSelectInfo, FrmSelectInfo);
  with FrmSelectInfo do
  try
    crsComType.Close;
    crsComType.CommandText := 'SELECT ' + GetClientRowSetColumns(crsComType) + ' FROM bas_complain_type WHERE group_code=' + QuotedStr(userInfo.groupCode) +
      ' AND com_type=' + QuotedStr(comType);
    crsComType.Open;
    SetTabSheetVisible(2);
    if ShowModal = mrOk then
    begin
      errorType := crsComType.FieldByName('error_type').AsString;
      responLevel := crsComType.FieldByName('respon_level').AsString;
    end;
  finally
    FreeAndNil(FrmSelectInfo);
  end;
end;
end.
